package org.jeecg.modules.project.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayDataDataserviceAdReportdataQueryModel;
import com.alipay.api.request.AlipayDataDataserviceAdReportdataQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceAdReportdataQueryResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.Liu
 * @date 2024/05/15
 * @Description 广告投放数据通用接口
 */
public class AlipayDataDataserviceAdReportdataQuery {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayDataDataserviceAdReportdataQueryRequest request = new AlipayDataDataserviceAdReportdataQueryRequest();
        AlipayDataDataserviceAdReportdataQueryModel model = new AlipayDataDataserviceAdReportdataQueryModel();

        // 设置数据查询结束时间
        model.setEndDate("20231010");

        // 设置广告投放平台生成的创意ID
        List<String> creativeIdList = new ArrayList<String>();
        creativeIdList.add("154579112284777");
        model.setCreativeIdList(creativeIdList);

        // 设置数据汇总方式
        model.setQueryType("SUM");

        // 设置广告投放平台生成的单元ID
        List<String> groupIdList = new ArrayList<String>();
        groupIdList.add("1001");
        model.setGroupIdList(groupIdList);

        // 设置投放方式
        model.setSceneType("INTELLIGENT");

        // 设置登录用户支付宝统一ID
        model.setAlipayPid("20885410270xxxxx");

        // 设置商家标志
        model.setPrincipalTag("88e3d3cc62bf4af194ef338d7xxxxx");

        // 设置分页查询的页码从1开始
        model.setCurrent(1L);

        // 设置广告投放平台生成的计划ID
        List<String> planIdList = new ArrayList<String>();
        planIdList.add("1001");
        model.setPlanIdList(planIdList);

        // 设置权限token
        model.setBizToken("e09d869511189c24ce13fe3xxxxxx");

        // 设置广告投放平台生成的订单ID
        List<String> orderIdList = new ArrayList<String>();
        orderIdList.add("1001");
        model.setOrderIdList(orderIdList);

        // 设置数据维度
        model.setAdLevel("PLAN");

        // 设置投放模式（订单/计划/单元/创意id_list都为空时此参数有效）
        model.setDeliveryMode("STANDARD_TOUFANG");

        // 设置数据查询开始时间
        model.setStartDate("20231001");

        // 设置分页查询时每页返回条数最大1万
        model.setPageSize(1L);

        request.setBizModel(model);
        AlipayDataDataserviceAdReportdataQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey = "<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->";
        String alipayPublicKey = "<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("<-- 请填写您的AppId，例如：2019091767145019 -->");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

}
